using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ProductController : ControllerBase
    {
        private readonly IProductService _productService;

        public ProductController(IProductService productService)
        {
            _productService = productService;
        }

        [HttpGet]
        public async Task<ActionResult<ApiResponse<List<Product>>>> GetAll()
        {
            try
            {
                var products = await _productService.GetAllAsync();
                return Ok(ApiResponse<List<Product>>.Ok(products, "获取产品列表成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<List<Product>>.Error($"获取产品列表失败: {ex.Message}"));
            }
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<ApiResponse<Product>>> GetById(Guid id)
        {
            try
            {
                var product = await _productService.GetByIdAsync(id);
                if (product == null)
                    return NotFound(ApiResponse<Product>.Error("产品不存在"));

                return Ok(ApiResponse<Product>.Ok(product, "获取产品成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<Product>.Error($"获取产品失败: {ex.Message}"));
            }
        }

        [HttpGet("category/{categoryId}")]
        public async Task<ActionResult<ApiResponse<List<Product>>>> GetByCategory(Guid categoryId)
        {
            try
            {
                var products = await _productService.GetByCategoryAsync(categoryId);
                return Ok(ApiResponse<List<Product>>.Ok(products, "获取分类产品成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<List<Product>>.Error($"获取分类产品失败: {ex.Message}"));
            }
        }

        [HttpGet("search")]
        public async Task<ActionResult<ApiResponse<List<Product>>>> Search([FromQuery] string keyword)
        {
            try
            {
                var products = await _productService.SearchAsync(keyword);
                return Ok(ApiResponse<List<Product>>.Ok(products, "搜索产品成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<List<Product>>.Error($"搜索产品失败: {ex.Message}"));
            }
        }

        [HttpPost]
        public async Task<ActionResult<ApiResponse<Product>>> Create(Product product)
        {
            try
            {
                var createdProduct = await _productService.CreateAsync(product);
                return Ok(ApiResponse<Product>.Ok(createdProduct, "创建产品成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<Product>.Error($"创建产品失败: {ex.Message}"));
            }
        }

        [HttpPut]
        public async Task<ActionResult<ApiResponse<Product>>> Update(Product product)
        {
            try
            {
                var updatedProduct = await _productService.UpdateAsync(product);
                return Ok(ApiResponse<Product>.Ok(updatedProduct, "更新产品成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<Product>.Error($"更新产品失败: {ex.Message}"));
            }
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResponse<bool>>> Delete(Guid id)
        {
            try
            {
                var result = await _productService.DeleteAsync(id);
                if (!result)
                    return NotFound(ApiResponse<bool>.Error("产品不存在"));

                return Ok(ApiResponse<bool>.Ok(true, "删除产品成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<bool>.Error($"删除产品失败: {ex.Message}"));
            }
        }
    }
} 